package ;

import flixel.group.FlxTypedGroup;
import flixel.group.FlxGroup;
import flixel.util.FlxColor;
import flixel.util.FlxPoint;
import flixel.text.FlxText;
import flixel.FlxSprite;


class InvHolder extends FlxTypedGroup<FlxSprite>
{
	private var _itemBase:FlxSprite;
	public var _hoverText:FlxSprite;
	public var _hoverTextBack:FlxSprite;
	public var _itemImageSrc:String;
	public var _count:Int;
	//private var _unlocked:Bool = false;
	public var _itemCount:FlxText;
	public var x:Int;
	public var y:Int;
	public var _itemName:String;
	
	public function new(X:Int, Y:Int, name:String, imgPath:String, c:Int)
	{
		super();
		x = X;
		y = Y;
		_itemName = name;
		_itemImageSrc = imgPath;
		_itemBase = new FlxSprite(X, Y, (c == -1) ? AssetPaths.no_item__png : "assets/images/" + imgPath);
		_itemBase.scrollFactor.set();
		_itemBase.setGraphicSize(80, 80);
		_itemBase.updateHitbox();
		add(_itemBase);
		_count = c;
		if(_count == -2) {
			_itemCount = new FlxText(x + 40, y + 80, 40, "*", 24);
		}
		else {
			_itemCount = new FlxText(x + 40, y + 80, 40, Std.string(c), 24);
		}
		_itemCount.color = FlxColor.WHITE;
		_itemCount.scrollFactor.set();
		add(_itemCount);
		
		var nameCount:Int = _itemName.length;
		_hoverText = new FlxText(x, y - 15, nameCount * 13, _itemName, 16);
		_hoverText.color = FlxColor.BLACK;
		_hoverText.scrollFactor.set();
		_hoverTextBack = new FlxSprite(x, y - 15);
		_hoverTextBack.makeGraphic(nameCount * 13, 20, FlxColor.WHITE);
		_hoverTextBack.scrollFactor.set();
		add(_hoverTextBack);
		add(_hoverText);
		// needs to start invisible and becomes visible when the mouse/finger moves over the item
		_hoverText.visible = false;
		_hoverTextBack.visible = false;
		//add(_hoverTextArea);
	}

	override public function update():Void {
		super.update();
		if (_count == -1) {
			_itemCount.visible = false;
		}
		else {
			_itemCount.visible = true;
			if (_count == 0) {
				_itemBase.alpha = .3;
			}
			else {
				_itemBase.alpha = 1;
			}
		}
	}
	
	public function getItem():Item
	{		
		if(_count != -2) {
			_count--;
		}
		if(_count >= 0) {
			_itemCount.text = Std.string(_count);
		}
		return new Item(_itemBase.x, _itemBase.y, _itemName, _itemImageSrc, this);
	}
	
	public function incrementCount():Void
	{
		if (_count >= 0)
		{
			_count++;
		}
		else if(_count == -1)
		{
			_count = 1;
			_itemBase.loadGraphic("assets/images/" + _itemImageSrc);
			_itemBase.setGraphicSize(80, 80);
			_itemBase.updateHitbox();
		}
		if(_count >= 0) {
			_itemCount.text = Std.string(_count);
		}
	}
	
	public function overlapsPoint(point:FlxPoint)
	{
		return _itemBase.overlapsPoint(point);
	}
	
	public function highlightTextOn():Void {
		_hoverText.visible = true;
		_hoverTextBack.visible = true;
	}
	
	public function highlightTextOff():Void {
		_hoverText.visible = false;
		_hoverTextBack.visible = false;
	}
}